package com.lmb.todo.service.impl;

import com.lmb.todo.dao.UserDao;
import com.lmb.todo.pojo.Folder;
import com.lmb.todo.pojo.Group;
import com.lmb.todo.pojo.Todo;
import com.lmb.todo.pojo.User;
import com.lmb.todo.service.FolderService;
import com.lmb.todo.service.GroupService;
import com.lmb.todo.service.TodoService;
import com.lmb.todo.utils.StringUtil;
import com.lmb.todo.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lmb.todo.service.UserService;
import org.springframework.transaction.annotation.Transactional;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private FolderService folderService;

    @Autowired
    private GroupService groupService;

    @Autowired
    private TodoService todoService;

    public UserServiceImpl() {}

    @Override
    public User getById(Integer id) {
        if (id <= 0) {
            return null;
        }

        User user = userDao.getById(id);
        if (user != null) {
            user.setPassword(null);
            user.setId(null);
        }
        return user;
    }

    @Override
    public User getByUsername(String username) {
        User user = userDao.getByUsername(username);
        if (user != null) {
            user.setPassword(null);
            user.setId(null);
        }
        return user;
    }

    @Override
    public User getByUsernameAndPassword(String username, String password) {
        if (!StringUtil.checkUsername(username) || !StringUtil.checkPassword(password)) {
            return null;
        }
        return userDao.getByUsernameAndPassword(username, password);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer add(User user) {
        String createTime = DateUtil.getCurrentDateStr();
        user.setCreateTime(DateUtil.getCurrentDateStr());
        userDao.add(user);

        Integer userId = user.getId();

        Folder folder = new Folder(userId, "文件夹", "可用于收纳分组", createTime, null);

        folderService.add(folder);
        Integer folderId = folder.getId();

        Group group1 = new Group(userId, null, "List", "我的一天", "今天要做什么?", createTime, null, 0);
        Group group2 = new Group(userId, null, "List", "重要", "重要的事情", createTime, null, 0);
        Group group3 = new Group(userId, null, "List", "今天吃什么", "", createTime, null, 0);
        Group group4 = new Group(userId, null, "List", "娱乐活动安排", "放松身心!", createTime, null, 0);
        Group group5 = new Group(userId, folderId, "List", "计划内", "计划安排", createTime, null, 0);
        Group group6 = new Group(userId, folderId, "List", "学习", "day day up!", createTime, null, 0);

        groupService.add(group1);
        groupService.add(group2);
        groupService.add(group3);
        groupService.add(group4);
        groupService.add(group5);
        groupService.add(group6);

        Todo todo1 = new Todo(group1.getId(), 0, 1, "我的待办, 今天要加倍努力!", "", createTime, null, null);
        Todo todo2 = new Todo(group1.getId(), 1, 0, "待办以完成, 继续执行下一个计划!", "", createTime, null, null);

        todoService.add(todo1, false);
        todoService.add(todo2, false);

        return 1;
    }

    @Override
    public Integer update(User user) {
        return null;
    }

    @Override
    public Integer deleteById(Integer id) {
        return null;
    }
}
